package com.freeter.modules.controller;

import com.freeter.annotation.Login;
import com.freeter.common.utils.R;
import com.freeter.interceptor.LoginInterceptor;
import com.freeter.modules.gjs.entity.User;
import com.freeter.modules.gjs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/app/auth")
public class AuthController {
    @Autowired
    private UserService userService;

    @Login
    @GetMapping("/user-info")
    @ResponseBody
    public R userInfo(HttpServletRequest request) {
        Object userIdObj = request.getAttribute(LoginInterceptor.USER_KEY);
        if (userIdObj == null) {
            return R.error(401, "未登录");
        }
        Long userId = (Long) userIdObj;
        User user = userService.selectById(userId);
        if (user == null) {
            return R.error(404, "用户不存在");
        }
        user.setPassword(null);
        return R.ok().put("data", user);
    }
} 